package com.ls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.common.R;
import com.ls.entity.Employee;
import com.ls.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @author atom.hu
 * @version V1.0
 * @Package com.ls.controller
 * @date 2021/10/13 14:46
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 处理用户登录的请求
     * @param employee 封装客户端发送过来的用户名和密码
     * @param session 保存用户登录成功的id
     * @return 返回响应结果
     */
    @PostMapping("/login")//post请求
    //@requestBody将json封装成员工对象
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        //DigestUtils类在spring-core包中，注意导包。
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        //2、根据页面提交的用户名username和密码password查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername()) //用户名
                .eq(Employee::getPassword, password);//密码
        Employee emp = employeeService.getOne(queryWrapper);
        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("账号或者密码错误");
        }
        ;
        //4、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //5、登录成功，将员工id存入Session并返回登录成功结果
        session.setAttribute("employee", emp.getId());
        return R.success(emp); //注意不要返回错对象了
    }
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("登出成功");
    }

    /**
     * 保存员工信息
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        //1 设置初始密码并加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        //2 设置保存和修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //3 设置本次操作保存和修改的用户id
//        Long empId = (Long) session.getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        //4 调用employeeService，保存员工信息
        employeeService.save(employee);
        return R.success("保存员工成功");
    }

    /**
     * 分页查询员工信息
     * @param page 当前页
     * @param pageSize 每页条数
     * @param name 查询条件
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //1 定义分页对象,设置分页参数
        Page<Employee> pageInfo=new Page<>(page,pageSize);
        //2 定义查询条件
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        //2.1 设置模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        //2.2 设置降序排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //3调用employeeService分页查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改用户信息
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        //1 设置修改时间
//        employee.setUpdateTime(LocalDateTime.now());
        //2 设置当前执行修改操作的用户id
//        Long empId = (Long) session.getAttribute("employee");
//        employee.setUpdateUser(empId);
        //3 调用employeeService方法修改用户信息
        employeeService.updateById(employee);
        //4 响应结果
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //根据id查询员工信息
        Employee employee = employeeService.getById(id);
        //响应不同结果
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有查到员工信息");
    }
}
